/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Dao.actorDao;
import Dao.directorDao;
import Dao.movieDao;
import Entity.Actor;
import Entity.Director;
import Entity.Movie;
import java.util.List;
import javax.persistence.*;

/**
 *
 * @author Leoz
 */
public class searchControl {

    private actorDao actor;
    private directorDao director;
    private movieDao movie;
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaPU");

    public searchControl() {
        actor = new actorDao();
        director = new directorDao();
        movie = new movieDao();
    }

    public List<Movie> searchbyName(String name) {
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = null;
        try {
            em.getTransaction().begin();
            movies = movie.searchbyName(name, em); 
        } catch (NoResultException ex)  {
            
        } finally {
            em.close();
            return movies;
        }

    }
    
    public List<Movie> searchbyGenre(String name) {
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = null;
        try {
            em.getTransaction().begin();
            movies = movie.searchbyName(name, em); 
        } catch (NoResultException ex)  {
            
        } finally {
            em.close();
            return movies;
        }
    }
    
    public List<Movie> searchbyDirector(String name) {
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = null;
        try {
            em.getTransaction().begin();
            Director dir = director.searchbyName(name, em);
            movies = movie.searchbyDirector(dir.getIdDirector(), em); 
        } catch (NoResultException ex)  {
        } finally {
            em.close();
            return movies;
        }
    }
    
    public List<Movie> searchbyActor(String name) {
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = null;
        try {
            em.getTransaction().begin();
            Actor act = actor.searchbyName(name, em);
            movies = movie.searchbyActor(act.getIdActor(), em); 
        } catch (NoResultException ex)  {
        } finally {
            em.close();
            return movies;
        }
    }
}
